#include "md5.h"
#include "encodedict.h"
#include "common.h"

#define MD5_STR_SIZE_MAX 32

const char  *NAME_LIST[] = {
    "an",       "bai",      "bao",      "bi",       "bian",     "bo",       "cai",      "cao",      "ceng",
    "cha",      "chai",     "chang",    "che",      "chen",     "cheng",    "cong",     "cui",      "dai",      "dan",
    "deng",     "di",       "ding",     "du",       "duan",     "fan",      "fang",     "fei",      "feng",     "fu",
    "gan",      "gao",      "ge",       "geng",     "gong",     "gu",       "guan",     "gui",      "guo",      "han",
    "hao",      "he",       "hong",     "hou",      "hu",       "hua",      "huang",    "huo",      "ji",       "jia",
    "jiang",    "jiao",     "jie",      "jin",      "jing",     "kang",     "ke",       "lai",      "lan",      "li",
    "liang",    "liao",     "lin",      "ling",     "liu",      "long",     "lu",       "luo",      "lv",       "ma",
    "mao",      "mei",      "meng",     "miao",     "min",      "mo",       "mou",      "ni",       "nie",      "ning",
    "niu",      "ou",       "pan",      "pang",     "pei",      "peng",     "pu",       "qi",       "qian",     "qiang",
    "qiao",     "qin",      "qiu",      "qu",       "ran",      "ren",      "ruan",     "shao",     "shen",     "sheng",
    "shi",      "shu",      "si",       "song",     "su",       "sui",      "sun",      "tan",      "tang",     "tao",
    "tian",     "tong",     "tu",       "wan",      "wang",     "wei",      "wen",      "weng",     "wu",       "xi",
    "xia",      "xiang",    "xiao",     "xie",      "xin ",     "xing",     "xiong",    "xu",       "xue",      "yan",
    "yang",     "yao",      "ye",       "yi",       "yin",      "you",      "yu",       "yuan",     "yue",      "zhan",
    "zhang",    "zhao",     "zhen",     "zheng",    "zhi",      "zhong",    "zhou",     "zhu",      "zhuang",   "zou",
    "zu"
};

void saveKeyDict(FILE *fp, const char* key,  const char* value)
{
	assert(fp);
	assert(key);
	assert(value);
	fprintf(fp, "%s=%s\n", key, value);
}

void generateMd5(const char *src_str, char *md5)
{
	const char*tmp;
	string s = MD5(src_str).toStr();
	tmp = s.c_str();
	sprintf(md5, "%s",  tmp);
}


void generateNameDict(FILE * name_fp)
{
    std::cout <<"正在生成名字信息口令字典......."<<std::endl;
    int i = 0;
    const char **p = NULL;
    const char *key;
    char value[33] = {0};

    p = NAME_LIST;

    for (i=0; i<150; i++) {
    	key = *p;
		generateMd5(key, value);
		saveKeyDict(name_fp, key, value);
		++p;
    }
    std::cout <<"名字信息口令字典生成成功！！！！！！"<<std::endl;
}
void generateNumDict(FILE *num_fp, FILE *blend_fp)
{
	int start = 0;
	int end   = 999999;
	int num = 0;
	char key[200];
	char value[200];

	std::cout <<"正在生成数字信息口令字典......."<<std::endl;

	for (num=start; num<=end; num++){
		sprintf(key, "%d", num);
		generateMd5(key, value);
		saveKeyDict(num_fp, key, value);
		generateBlendDict(blend_fp, key);
	}
	std::cout <<"数字信息口令字典生成成功！！！！！！"<<std::endl;
}

void generatePhoneDict(FILE *phone_fp, FILE *blend_fp)
{
	int start = 8000000;
	int end   = 9999999;
	int phone = 0;
	char key[200];
	char value[200];

	std::cout <<"正在生成电话信息口令字典......."<<std::endl;

	for (phone=start; phone<=end; phone++){
		sprintf(key, "%d", phone);
		generateMd5(key, value);
		saveKeyDict(phone_fp, key, value);
		generateBlendDict(blend_fp, key);
	}
	std::cout <<"电话信息口令字典生成成功！！！！！！"<<std::endl;
}

void generateBlendDict(FILE *blend_fp,  char*other)
{
	int i = 0;
	const char **p = NULL;
	const char *key;
	char value[200];
	char new_key[200];
	char *save_str;

	p = NAME_LIST;

	for (i=0; i<150; i++) {
		key = *p;
		sprintf(new_key, "%s%s", key, other);
		generateMd5(key, value);
		saveKeyDict(blend_fp, new_key, value);
		++p;
	}
}

void generateBirthdayDict(FILE *birthday_fp,  FILE *blend_fp)
{
	int start = 1900;
	int end   = 2015;
	int y = 0;
	int m = 0;
	int d = 0;
	char key[200];
	char value[200];

	std::cout <<"正在生成生日信息口令字典......."<<std::endl;

	for (y=start; y<=end; y++){
		for (m=0; m<=12; m++) {
			for (d=0; d<=31; d++) {
				sprintf(key, "%d%02d%02d", y, m, d);
				generateMd5(key, value);
				saveKeyDict(birthday_fp, key, value);
				generateBlendDict(blend_fp, key);
			}
		}
	}

	std::cout <<"生日信息口令字典生成成功！！！！！！"<<std::endl;
}

void encondeDict(void)
{
	FILE *name_fp = NULL;
	FILE *birthday_fp = NULL;
	FILE *phone_fp = NULL;
	FILE *num_fp = NULL;
	FILE *blend_fp = NULL;


	FILE *fp = NULL;
	fp =  fopen(DICT_PATH_DIR, "r");
	if (!fp) {
		 std::cout<<"创建口令字典目录"<<std::endl;
		 char cmd[1024] = {0};
		 sprintf(cmd, "mkdir -p %s", DICT_PATH_DIR);
		 system(cmd);
	}
	name_fp = fopen(NAME_DICT_PATH, "w+");
	birthday_fp = fopen(BIRTHDAY_DICT_PATH, "w+");
	blend_fp = fopen(BLEND_DICT_PATH, "w+");
	num_fp = fopen(NUM_DICT_PATH, "w+");
	phone_fp = fopen(PHONE_DICT_PATH, "w+");

	generateNameDict(name_fp);
    generateBirthdayDict(birthday_fp, blend_fp);
	generateNumDict(num_fp, blend_fp);
    generatePhoneDict(phone_fp, blend_fp);

	fclose(name_fp);
	fclose(birthday_fp);
	fclose(blend_fp);

}

void generatePassword()
{
	char password[100]={0};
	char new_password[100]={0};

	std::cout <<"请输明文密码：";
	scanf("%s",  password);
	std::cout <<"正在加密.................."<<std::endl;
	generateMd5(password, new_password);
	printf("生成密码成功，加密后密码为：%s\n", new_password);
}
